package view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import entity.User;
import service.user.UserService;
import service.user.UserServiceImpl;
import view.CommonView.CommonJFrame;
import view.CommonView.CommonJPanel;


/**
 * @Author: Lin Hongwei
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 创建登录界面 步骤： 1. 用JFrame作为顶层容器 2. 设置容器的基础配置 3. 创建JPanel作为中间容器
 *
 */
public class LoginView extends CommonJFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 单例模式：只允许一个实例存在
	public static LoginView instance = new LoginView();

	// private BgJPanel contentPane;
	private JPanel contentPane;
	
	private static User user = null;

	public LoginView() {
		// 1.设置基础配置
//		super("图书管理系统");
		setSize(700, 500);// 设置标题
		setLocationRelativeTo(null);// 设置相对于哪个组件的位置（null相当于屏幕中央）
		
		
		// 2.初始化面板
		contentPane = new CommonJPanel(CommonJPanel.TYPE_BG);
		contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 50));
		contentPane.setBackground(new Color(255, 255, 255));
		setContentPane(contentPane);
		// 设置空白边界,相当于内边距（EmptyBorder：空白的边框《top,left,bottom,right》）
		contentPane.setBorder(new EmptyBorder(20, 100, 10, 100));

		// 3.创建标题面板
		CommonJPanel titleJPanel = new CommonJPanel(CommonJPanel.TYPE_TRANSPARENT);
		contentPane.add(titleJPanel, BorderLayout.NORTH);// 独占一行
		// 3.1 设置标题标签
		JLabel titleLabel = new JLabel("图书管理系统");// 设置标题
		URL iconUrl = LoginView.class.getResource("/img/book.png");// 获取到图片地址
		titleLabel.setIcon(new ImageIcon(iconUrl));
		titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 27));
		titleJPanel.add(titleLabel);

		// 4. 创建输入框
		CommonJPanel fieldPanel = new CommonJPanel(CommonJPanel.TYPE_TRANSPARENT);
		// 行数 , 列数 ,水平 , 竖直 网格间隙
		fieldPanel.setLayout(new GridLayout(2, 2, 0, 30));
		contentPane.add(fieldPanel, BorderLayout.CENTER);
		// 4.1 创建用户名标签
		JLabel idLabel = new JLabel("用户名：");
		idLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(idLabel);
		// 4.2 创建用户名输入框
		JTextField userField = new JTextField();
		// 设置大小，setPreferredSize类似于自适应
		userField.setPreferredSize(new Dimension(150, 40));
		userField.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(userField);

		// 4.3 创建密码标签
		JLabel passwordLabel = new JLabel("密　码：");
		passwordLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(passwordLabel);
		// 4.4 创建密码输入框
		JPasswordField passwordField = new JPasswordField();
		passwordField.setPreferredSize(new Dimension(150, 40));
		passwordField.setFont(new Font("微软雅黑", Font.BOLD, 20));
		fieldPanel.add(passwordField);

		// 5. 创建按钮面板
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridLayout(1, 2, 80, 0));
		buttonPanel.setOpaque(false);// 设置背景色为透明
		contentPane.add(buttonPanel);
		// 5.1 创建登录按钮
		JButton login = new JButton("登录");
		login.setFont(new Font("微软雅黑", Font.BOLD, 15));
		buttonPanel.add(login);
		// 5.2 创建重置按钮
		JButton reset = new JButton("重置");
		reset.setFont(new Font("微软雅黑", Font.BOLD, 15));
		buttonPanel.add(reset);
		
		
		userField.setText("1001");
		passwordField.setText("1234");
		

		// 5.3 监听登录按钮事件
		login.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			
				String name = userField.getText();
				String pass = new String(passwordField.getPassword());
				System.out.println("用户名:" + name + ",密码:" + pass);
				if (name.equals("") || pass.equals("")) {
					JOptionPane.showMessageDialog(LoginView.this, "用户名与密码不能为空！");
					return;
				}
				
				UserService userService = new UserServiceImpl();
				user = userService.login(name, pass);
				if(user!=null) {
					MainView.instance.setVisible(true);
					setVisible(false);
					return;
				}
				JOptionPane.showMessageDialog(LoginView.this, "用户名与密码不正确！");
				return;
				

			}

		});

		reset.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				userField.setText("");
				passwordField.setText("");
			}

		});

		setVisible(true);// 显示

	}
	
	public static User getUser() {
		return user;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
